package com.cskaoyan.controller.admin;


import com.cskaoyan.bean.BasePageInfo;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.MarketStorage;
import com.cskaoyan.bean.param.CommonData;
import com.cskaoyan.service.admin.MarketStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Can
 * @since 2022/06/26 17:35
 */
@RestController
@RequestMapping("admin/storage")
public class AdminStorageController {
    @Autowired
    MarketStorageService marketStorageService;


    /**
     * 更新state
     * @return
     * @author LC_R-SH
     * @since 2022/06/27 20:38
     */
    @RequestMapping("update")
    public BaseRespVo update(@RequestBody MarketStorage marketStorage) {
        MarketStorage storage = marketStorageService.update(marketStorage);
        if (!ObjectUtils.isEmpty(storage)) {
            return BaseRespVo.ok(storage);
        }
        return BaseRespVo.invalidData("更新失败");
    }

    /**
     * 通过对象Key和对象名称查找对象
     *
     * @return 对象链表
     * @author LC_R-SH
     * @since 2022/06/27 9:26
     */
    @RequestMapping("list")
    public BaseRespVo list(String key, String name, BasePageInfo info) {

        CommonData<MarketStorage> data = marketStorageService.query(key, name, info);

        return BaseRespVo.ok(data);

    }


    /**
     * 用于对象存储  上传图片
     *
     * @param file
     * @param request
     * @return
     */
    @RequestMapping("create")
    public BaseRespVo create(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        BaseRespVo respVo = null;
        if (!file.isEmpty()) {
            respVo = marketStorageService.uploadImage(file);
        }
        return respVo;
    }

    /**
     * 系统管理-对象存储模块的删除功能。
     * @param marketStorage
     * @return
     */
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody MarketStorage marketStorage){

        Integer affactRows = marketStorageService.delete(marketStorage);
        if (affactRows == 1){
            return BaseRespVo.ok();
        }
        return BaseRespVo.invalidData();

    }

}


